package com.mj.starsignpair.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.starsignpair.R;
import com.mj.starsignpair.service.StarsignpairService;
import com.mj.starsignpair.util.NetUtils;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 星座配对测试
 * @author zhaominglei
 * @date 2015-4-9
 * 
 */
public class MainActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	private StarsignpairService starsignpairService = new StarsignpairService();
	private boolean isExit = false;
	private TimerTask timerTask;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private Spinner boySpinner; //男生星座选择框
	private String boyStar; //男生星座
	private Spinner girlSpinner; //女生星座选择框
	private String girlStar; //女生星座
	private Button queryBtn; //配对测试
	private ProgressBar queryProgressBar; //查询进度条
	private TextView resultView; //结果
	private Button appOffersButton; //推荐应用
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.miniAdLinearLayout);
		boySpinner = (Spinner)findViewById(R.id.starsignpair_boy_spinner);
		girlSpinner = (Spinner)findViewById(R.id.starsignpair_girl_spinner);
		queryBtn = (Button)findViewById(R.id.starsignpair_query_btn);
		queryProgressBar = (ProgressBar)findViewById(R.id.starsignpair_query_progressbar);
		resultView = (TextView)findViewById(R.id.starsignpair_result);
		appOffersButton = (Button)findViewById(R.id.appOffersButton);
		
		queryBtn.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		appControl = AppControl.getInstance();
		appControl.init(MainActivity.this, "e21e52577f7272229RK2WgnnbMs3CIByBFSmrqtpzfjNS+dCOu4tkmMG9r/A6/lH7w", "木蚂蚁");
		appControl.loadPopAd(MainActivity.this);
		appControl.showPopAd(MainActivity.this, 60 * 1000);
		appControl.showInter(MainActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(MainActivity.this, "e21e52577f7272229RK2WgnnbMs3CIByBFSmrqtpzfjNS+dCOu4tkmMG9r/A6/lH7w", "木蚂蚁");
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.starsignpair_query_btn:
			boyStar = (String)boySpinner.getSelectedItem();
			if (boyStar == null || boyStar.equals("")) {
				Toast.makeText(getApplicationContext(), R.string.starsignpair_boy_prompt, Toast.LENGTH_SHORT).show();
				return;
			}
			girlStar = (String)girlSpinner.getSelectedItem();
			if (girlStar == null || girlStar.equals("")) {
				Toast.makeText(getApplicationContext(), R.string.starsignpair_girl_prompt, Toast.LENGTH_SHORT).show();
				return;
			}
			resultView.setText("");
			
			getStarsignpairInfo();
			break;
			
		case R.id.appOffersButton:
			access.openWALL(MainActivity.this);
			break;
			
		default:
			break;
		}
	}

	private void getStarsignpairInfo() {
		if (NetUtils.isConnected(getApplicationContext())) {
			queryProgressBar.setVisibility(View.VISIBLE);
			new StarsignpairInfoAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	public class StarsignpairInfoAsyncTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			return starsignpairService.getStarsignpairInfo(boyStar, girlStar);
		}

		@Override
		protected void onPostExecute(String result) {
			queryProgressBar.setVisibility(View.GONE);
			if (result != null && !result.equals("")) {
				resultView.setText(Html.fromHtml(result));
			} else {
				Toast.makeText(getApplicationContext(), R.string.starsignpair_error1, Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}
}
